﻿using System;
using BP.DA;
using BP.En;
namespace BP.WF.DTS
{
    /// <summary>
    /// 同步待办时间戳 的摘要说明
    /// </summary>
    public class DTS_GenerWorkFlowTimeSpan : Method
    {
        /// <summary>
        /// 同步待办时间戳
        /// </summary>
        public DTS_GenerWorkFlowTimeSpan()
        {
            this.Title = "同步待办时间戳,状态,流程注册表的时间段(本周，上周，2周以前，3其他。).";
            this.Help = "该方法每周一自动执行，如果不能自动执行就手动执行";
            this.GroupName = "流程自动执行定时任务";

        }
        /// <summary>
        /// 设置执行变量
        /// </summary>
        /// <returns></returns>
        public override void Init()
        {
            //this.Warning = "您确定要执行吗？";
            //HisAttrs.AddTBString("P1", null, "原密码", true, false, 0, 10, 10);
            //HisAttrs.AddTBString("P2", null, "新密码", true, false, 0, 10, 10);
            //HisAttrs.AddTBString("P3", null, "确认", true, false, 0, 10, 10);
        }
        /// <summary>
        /// 当前的操纵员是否可以执行这个方法
        /// </summary>
        public override bool IsCanDo
        {
            get
            {
                return true;
            }
        }
        /// <summary>
        /// 执行
        /// </summary>
        /// <returns>返回执行结果</returns>
        public override object Do()
        {
            DateTime today = DateTime.Now.Date; // 获取当前日期，不包含时间部分
            int diff = (7 + (today.DayOfWeek - DayOfWeek.Monday)) % 7;// 获取周一的日期，这里假设周一为每周的第一天
            DateTime startOfWeek = today.AddDays(-1 * diff).Date; // 本周一
            DateTime endOfWeek = startOfWeek.AddDays(6); // 本周日
 
            DateTime lastWeekStart = startOfWeek.AddDays(-7); // 上周一
            DateTime lastWeekEnd = endOfWeek.AddDays(-7); // 上周日
 
            DateTime twoWeeksAgoStart = lastWeekStart.AddDays(-7); // 上上周一
            DateTime twoWeeksAgoEnd = lastWeekEnd.AddDays(-7); // 上上周日

            //默认都设置为本周
            string sql = $"UPDATE WF_GenerWorkFlow SET TSpan = {((int)TSpan.ThisWeek).ToString()}";
            DBAccess.RunSQL(sql);

            //设置为上周.
            sql = $"UPDATE WF_GenerWorkFlow SET TSpan = {((int)TSpan.NextWeek).ToString()} WHERE RDT >= '{lastWeekStart.ToString("yyyy-MM-dd HH:mm:ss")}' AND RDT <= '{lastWeekEnd.ToString("yyyy-MM-dd HH:mm:ss")}'";
            DBAccess.RunSQL(sql);

            //把上周的，设置为两个周以前.
            sql = $"UPDATE WF_GenerWorkFlow SET TSpan = {((int)TSpan.TowWeekAgo).ToString()} WHERE RDT >= '{twoWeeksAgoStart.ToString("yyyy-MM-dd HH:mm:ss")}' AND RDT <= '{twoWeeksAgoEnd.ToString("yyyy-MM-dd HH:mm:ss")}'";
            DBAccess.RunSQL(sql);

            //把上周的，设置为更早.
            sql = $"UPDATE WF_GenerWorkFlow SET TSpan = {((int)TSpan.More).ToString()} WHERE RDT < '{twoWeeksAgoStart.ToString("yyyy-MM-dd HH:mm:ss")}'";
            DBAccess.RunSQL(sql);

            return "执行成功...";
        }
    }
}
